import pandas as pd  
import sqlite3  
import numpy as np  
import random  
from datetime import datetime, timedelta  
 
df = pd.read_csv('kaggle_diabetes.csv')  

data_to_insert = df.iloc[49:1050]  
 
random.seed(42)  
 
def generate_patient_id():  
    return random.randint(10000000, 99999999)  

def generate_random_timestamp(start_date, end_date):  
    start_timestamp = int(start_date.timestamp())  
    end_timestamp = int(end_date.timestamp())  
    random_timestamp = random.randint(start_timestamp, end_timestamp)  
    return datetime.fromtimestamp(random_timestamp)  
  
conn = sqlite3.connect('health_records.db')  
  
conn.execute('''CREATE TABLE IF NOT EXISTS records (  
                    patient_id INTEGER,  
                    timestamp TEXT,  -- 改为TEXT以存储字符串格式的时间戳  
                    pregnancies INTEGER,  
                    glucose INTEGER,  
                    bloodpressure INTEGER,  
                    skinthickness INTEGER,  
                    insulin INTEGER,  
                    bmi REAL,  
                    dpf REAL,  
                    age INTEGER,  
                    prediction INTEGER  
                )''')  
 
start_date = datetime(2024, 1, 1)  
end_date = datetime(2024, 11, 8)  
  
for index, row in data_to_insert.iterrows():  
    patient_id = generate_patient_id()  
    timestamp = generate_random_timestamp(start_date, end_date)  
  
    timestamp_str = timestamp.strftime('%Y-%m-%d %H:%M:%S')  
  
    conn.execute('''INSERT INTO records (patient_id, timestamp, pregnancies, glucose, bloodpressure,   
                    skinthickness, insulin, bmi, dpf, age, prediction)   
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',   
                 (patient_id, timestamp_str, row['Pregnancies'], row['Glucose'], row['BloodPressure'],   
                  row['SkinThickness'], row['Insulin'], row['BMI'], row['DiabetesPedigreeFunction'],   
                  row['Age'], row['Outcome']))  
 
conn.commit()  
conn.close()